// 使用前必须引用jquery.js
// 键盘按下时验证
/*
*target : 选择器
*regExp：密码规则（可选）
*dialogW：弹框宽度（可选）
*title：提示文字（可选）
*dialogUrl：dialog.css路径（可选）
*/
//checkpswDefault = {
//     target:'',
//     regExp:/[^\x00-\xff]|=|\[|\]|\\|;|'|\/|\.|,|\*|-|\+/,
//     dialogW:'',
//     title:'密码格式不正确，请重新输入',
//     dialogUrl:''
// }
jQuery.extend({

    // [/u4e00-/u9fa5]中文
    // 不能包括[^\x00-\xff]双字节字符
    // 不能包括  =  [  ]  \  ;  '  /  .  ,   *  -  +
    checkpsw: function (checkpswDefault) {
        var target = checkpswDefault.target ? checkpswDefault.target : '',
            regExp = checkpswDefault.regExp ? checkpswDefault.regExp : /[^\x00-\xff]|=|\[|\]|\\|;|'|\/|\.|,|\*|-|\+/,
            title = checkpswDefault.title ? checkpswDefault.title : '密码格式不正确，请重新输入',
            dialogW = checkpswDefault.dialogW ? checkpswDefault.dialogW : '',
            dialogUrl = checkpswDefault.dialogUrl ? checkpswDefault.dialogUrl : '';

        target.on('focus', function () {
            $(this).tooltip('destroy');
        }).on('keyup', function (event) {
            var $this = $(this);
            if (regExp.test($(this).val())) {
                if (event.keyCode == 16) return false;
                $.IsInclude($this, title, dialogW, dialogW, dialogUrl);
            } else {
                $(this).tooltip('destroy');
            }
        });
    },

    // 点击修改按钮是验证
    /*
    *target : 修改按钮选择器
    *regExp：密码规则（可选）
    *dialogW：弹框宽度（可选）
    *title：提示文字（可选）
    *dialogUrl：dialog.css路径（可选）
    *oldVal:原始密码选择器
    *newVal:新密码选择器
    *repeatNewVal:新密码重复选择器
    */
    //checkpswDefault = {
    //     target:'',
    //     oldVal:'',
    //     newVal:'',
    //     repeatNewVal:''
    //     dialogUrl:''
    // }
    checkpswSubmit: function (checkpswDefault, callback) {
        var target = checkpswDefault.target ? checkpswDefault.target : '',
            oldVal = checkpswDefault.oldVal ? checkpswDefault.oldVal : '',
            regExp = checkpswDefault.regExp ? checkpswDefault.regExp : /[^\x00-\xff]|=|\[|\]|\\|;|'|\/|\.|,|\*|-|\+/,
            title = checkpswDefault.title ? checkpswDefault.title : '密码格式不正确，请重新输入',
            newVal = checkpswDefault.newVal ? checkpswDefault.newVal : '',
            repeatNewVal = checkpswDefault.repeatNewVal ? checkpswDefault.repeatNewVal : '',
            dialogW = checkpswDefault.dialogW ? checkpswDefault.dialogW : '',
            dialogUrl = checkpswDefault.dialogUrl ? checkpswDefault.dialogUrl : '';
        var checkpswFlag = false;
        target.on('click', function () {
            if (oldVal.val() == '') {
                $.IsInclude(oldVal, '原始密码不能为空', dialogW, dialogW, dialogUrl);
                return false;
            } else if (regExp.test(oldVal.val())) {
                if (event.keyCode == 16) return false;
                $.IsInclude(oldVal, title, dialogW, dialogW, dialogUrl);
            } else if (oldVal.val() != '' && !regExp.test(oldVal.val()) && checkpswFlag) {
                oldVal.tooltip('destroy');
            }
            if (newVal.val() == '') {
                $.IsInclude(newVal, '新密码不能为空', dialogW, dialogW, dialogUrl);
                return false;
            } else if (regExp.test(newVal.val())) {
                if (event.keyCode == 16) return false;
                $.IsInclude(newVal, title, dialogW, dialogW, dialogUrl);
            } else if (!(newVal.val().length >= 6 && newVal.val().length <= 20)) {
                $.IsInclude(newVal, '新密码必须为6-20位', dialogW, dialogW, dialogUrl);
                return false;
            } else if (!/([a-zA-Z]+[0-9]+)|([0-9]+[a-zA-Z]+)/.test(newVal.val())) {
                $.IsInclude(newVal, '新密码必须包含数字和字母', dialogW, dialogW, dialogUrl);
                return false;
            } else if (oldVal.val() === newVal.val()) {
                $.IsInclude(newVal, '新密码不能与原始密码一致', dialogW, dialogW, dialogUrl);
                return false;
            } else {
                newVal.tooltip('destroy');
            }
            if (repeatNewVal.val() == '') {
                $.IsInclude(repeatNewVal, '重复密码不能为空', dialogW, dialogW, dialogUrl);
                return false;
            } else if (regExp.test(repeatNewVal.val())) {
                if (event.keyCode == 16) return false;
                $.IsInclude(repeatNewVal, title, dialogW, dialogW, dialogUrl);
            } else if (repeatNewVal.val() !== newVal.val()) {
                $.IsInclude(repeatNewVal, '新密码与重复新密码不一致', dialogW, dialogW, dialogUrl);
                return false;
            } else {
                repeatNewVal.tooltip('destroy');
            }

            // 验证原始密码callback
            if (typeof callback == 'function') {
                callback(oldVal, checkpswFlag);
                flag = true
            }
            ;
            // 原始密码不正确 放入callback函数内
            // if(oldVal.val() != '123456qwe') {
            //     $.IsInclude(oldVal,'原始密码不正确',dialogW,dialogW,dialogUrl);
            //     checkpswFlag = false;
            //     return false;
            // }else {
            //     checkpswFlag = true;
            // }

        });
    },
    Dialog: function (a, w, url) {
        // 使用弹框需在页面引入dialog.css，为引入需传入 dialog.css路径
        if (!isInclude('dialog.css')) {
            $('head').eq(0).append('<link rel="stylesheet" href="' + url + '">');
        }

        if ($('.dialog_msg_shade').length > 0) return false;
        var f = '<div class="dialog_msg_shade">\
                     <div class="dialog_msg_box">\
                         <div class="dialog_msg_hint">' + a + '</div>\
                         <button class="dialog_msg_btn">我知道了</button>\
                     </div>\
                </div>';
        $('body').append(f);
        if (w) $('.dialog_msg_box').width(w);

        $('.dialog_msg_btn').on('click', function () {
            $('.dialog_msg_shade').remove()
        });
    },
    IsInclude: function (THAT, title, dialog, dialogW, dialogUrl) {
        //  if((isInclude('bootstrap.js') || isInclude('bootstrap.min.js')) && (isInclude('bootstrap.css') || isInclude('bootstrap.min.css'))) {
        // bootstrap存在使用 tooltip提示
        THAT.tooltip({
            title: title,
            trigger: 'manual'
        }).tooltip('show');
        $('.tooltip-inner').css({'background-color': '#c33131'});
        $('.tooltip-arrow').css({'border-top-color': '#c33131'});
        /* }else {
             // 不存在bootstrap弹框提示
             $.Dialog(title,dialog,dialogW,dialogUrl);
         }*/
    }
});

// 验证页面是否引入指定js/css
function isInclude(name) {
    var js = /js$/i.test(name);
    var es = document.getElementsByTagName(js ? 'script' : 'link');
    for (var i = 0; i < es.length; i++)
        if (es[i][js ? 'src' : 'href'].indexOf(name) != -1) return true;
    return false;
}
